<?php
/*
 * Created on 2013-04-19
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
?>
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 *  User_group Model Class
 *
 */
class  Wishlist_model extends CI_Model 
{
	/**
	 * @var unknown_type
	 */
	var $id  ;  
	/**
	 * @var unknown_type
	 */
	var $fullname  ;  
	/**
	 * @var unknown_type
	 */
	var $company  ; 
	/**
	 * @var unknown_type
	 */
	var $address  ;  
	/**
	 * @var unknown_type
	 */
	var $phone;
	/**
	 * @var unknown_type
	 */
	var $fax;
	/**
	 * @var unknown_type
	 */
	var $email;
	/**
	 * @var unknown_type
	 */
	var $title;
	/**
	 * @var unknown_type
	 */
	var $content;
	/*
	 * @access	public
	 */
	 var $recei_date;
	/*
	 * @access	public
	 */
	 var $answer_date;
	/*
	 * @access	public
	 */
	 var $status;
	/*
	 * @access	public
	 */
	
	 function __construct(){
	 	parent::__construct();
	 }
		
	function insert($data){
		$date = date('Y-m-d H:i:s', time()); //date('Y-mm-dd h:i:s');
		$sql = "INSERT INTO gtb_wishlist (product_id, member_id, date_add, date_update)
				VALUES(".
				intval($data['product_id']).", " .
				intval($data['member_id']).", " .
				$this->db->escape($date) . ", " .
				$this->db->escape($date)
				.")";
		$this->db->query($sql);
		//return $this->db->insert_id(); 
		return $this->db->affected_rows(); 
	}
	
		
	function delete($member_id, $product_id = ''){
		$sql = '';
		
		if (!empty($product_id))
			$sql = "DELETE FROM gtb_wishlist WHERE product_id = " .intval($product_id) . ' AND member_id = ' .intval($member_id);
		else 
			$sql = "DELETE FROM gtb_wishlist WHERE member_id = " . intval($member_id);
			
		$this->db->query($sql);
		
		return $this->db->affected_rows(); 
	}
	
	
	function getWishlistMember($member_id, $product_id = 0){
		$sql = "";
		if (empty($product_id))
			$sql = "SELECT c.*, p.vi_name, p.en_name, p.price, p.image, p.date_add, p.code
					FROM gtb_wishlist c
					LEFT JOIN gtb_product p ON c.product_id = p.id_product
					WHERE member_id = ". intval($member_id) ." ORDER BY date_update DESC";
		else
			$sql = "SELECT *
				FROM gtb_wishlist WHERE member_id = ". intval($member_id) . " AND product_id = " . intval($product_id);
			
		//echo $sql;
		$query = $this->db->query($sql);
		$result = $query->result_array();
       	
		return $result;
	}
	
	
function countWishlistMember($member_id = 0){
		$result = null;
		
		$sql = "SELECT COUNT(*) as num_record  
					FROM gtb_wishlist WHERE member_id = ". $member_id ."
				";
		
		
		$query = $this->db->query($sql);
		$result = $query->result();
       	
		return $result[0]->num_record;
	}
	
	
	function getAll($offset = 0, $number_record = 10){
		
		$sql = "SELECT *
				FROM gtb_wishlist ORDER BY date_add DESC LIMIT $offset, $number_record";
		
		//echo $sql;
		$query = $this->db->query($sql);
		$result = $query->result_array();
       	
		return $result;
	}
	
	function countAll($member_id = 0){
		$result = null;
		
		$sql = "SELECT COUNT(*) as num_record  
					FROM gtb_wishlist 
				";
		
		
		$query = $this->db->query($sql);
		$result = $query->result();
       	
		return $result[0]->num_record;
	}
	
	
//	
}

// EOF

